package ru.spb.osll.myfit.client.log;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class RemoteLogger {
	private static LoggingServiceAsync loggingService = null;

	private static LoggingServiceAsync getLoggingService() {
		if (loggingService == null)
			loggingService = GWT.create(LoggingService.class);
		return (loggingService);
	}

	private static void logging(int level, final String message) {
		if (level > 0 && level <= 5) {

			getLoggingService().logClientIssue(level, message,
					new AsyncCallback<Object>() {
						public void onSuccess(Object result) {
							result = message;
						}

						public void onFailure(Throwable caught) {
							System.out.println("Error:" + caught.toString());
						}
					});
		}
	}

	public static void log(final int level, String message) {
		if ((level == 4) || (level == 5)) {
			GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
				public void onUncaughtException(Throwable t) {
					RemoteLogger.logging(level, "Uncaught Exception: "
							+ t.getMessage());
				}
			});
		} else
			RemoteLogger.logging(level, message);

	}
}